package com.baidumap.administrator.happystudy.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.baidumap.administrator.happystudy.base.JsonTools;
import com.baidumap.administrator.happystudy.base.Utils;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.ui.CurriculumSchedule1Activity;
import com.baidumap.administrator.happystudy.adapter.PopSetData1Adapter;
import com.baidumap.administrator.happystudy.adapter.PopSetData2Adapter;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.Map;


public class PopupWindowSetData1 {

	private PopupWindow mPopupWindow;
	private Button mAddPictureBtn1;
	private Button mAddPictureBtn2;
	private Button mAddPictureBtn3;

	public ImageLoader loader;
	private ListView mListView;
	private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	private PopSetData1Adapter mAdapter;
	private static int pageNo = 1;



	private ListView mListView1;
	private ArrayList<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
	private PopSetData2Adapter mAdapter1;
	private static int pageNo1 = 1;


	private static final int TAKE_PICTURE = 0;
	private static final int RESULT_LOAD_IMAGE = 1;
	private static final int CUT_PHOTO_REQUEST_CODE = 2;
	private static final int SELECTIMG_SEARCH = 3;
	private String path = "";
	private Uri photoUri;
	View customView;
	Context context;
	/*
	 * 加载图片
	 */
	public void initAddPicturePopuptWindow(final CurriculumSchedule1Activity context, View v) {
		this.context = context;
		LayoutInflater layoutInflater = LayoutInflater.from(context);
		customView = layoutInflater.inflate(
				R.layout.popwindow_set_data, null);
		//获取PopupWindow中View的宽高
		customView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
		int measuredWidth = customView.getMeasuredWidth();
		int measuredHeight = customView.getMeasuredHeight();
		if (null != mPopupWindow)
			mPopupWindow.dismiss();

		// 创建一个PopupWindow
		// 参数1：contentView 指定PopupWindow的内容
		// 参数2：width 指定PopupWindow的width
		// 参数3：height 指定PopupWindow的height
		mPopupWindow = new PopupWindow(customView, 0, 0, true);
		mPopupWindow.setOutsideTouchable(true);
		LinearLayout ll_viewArea = (LinearLayout) ((Activity) context)
				.findViewById(R.id.laytemp1);
		mPopupWindow.setWidth(ll_viewArea.getWidth());
		mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindow.setFocusable(true);
		mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);//
//		int[] location = new int[2];
//		v.getLocationOnScreen(location);
//
//
//		mPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0] + v.getWidth() / 2, location[1] - measuredHeight);
		// 自定义view添加触摸事件
		customView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// closePopupWindow();

				return false;
			}
		});
		customView.findViewById(R.id.dismiss).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mPopupWindow.dismiss();
			}
		});
		iniListView();
	}
	private void iniListView() {

		// LoadImage初始化
		loader = ImageLoader.getInstance();
		pageNo = 1;
		mListView = (ListView) customView.findViewById(R.id.list);

		mAdapter = new PopSetData1Adapter(context, loader);
		new MyTask().execute("", "" + pageNo);// 线程加载json数据

		pageNo1 = 1;
		mListView1 = (ListView) customView.findViewById(R.id.list1);

		mAdapter1 = new PopSetData2Adapter(context, loader);
		new MyTask1().execute("", "" + pageNo1);// 线程加载json数据


	}
	class MyTask1 extends
			AsyncTask<String, Void, ArrayList<Map<String, Object>>> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();

		}

		@Override
		protected ArrayList<Map<String, Object>> doInBackground(
				String... params) {

			ArrayList<Map<String, Object>> mList = null;
			mList = (ArrayList<Map<String, Object>>) JsonTools.listKeyMaps(
					"list", Utils.HOUSE_JSON);
			return mList;
		}

		protected void onPostExecute(ArrayList<Map<String, Object>> result) {
			// TODO Auto-generated method stub

			list1.addAll(result);//

			mAdapter1.bindData(list1);
			if (pageNo1 == 1) {

				mListView1.setAdapter(mAdapter1);
			}
			pageNo1++;
			mAdapter1.notifyDataSetChanged();
			super.onPostExecute(result);

		}
	}
	class MyTask extends
			AsyncTask<String, Void, ArrayList<Map<String, Object>>> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();

		}

		@Override
		protected ArrayList<Map<String, Object>> doInBackground(
				String... params) {

			ArrayList<Map<String, Object>> mList = null;
			mList = (ArrayList<Map<String, Object>>) JsonTools.listKeyMaps(
					"list", Utils.HOUSE_JSON);
			return mList;
		}

		protected void onPostExecute(ArrayList<Map<String, Object>> result) {
			// TODO Auto-generated method stub

			list.addAll(result);//

			mAdapter.bindData(list);
			if (pageNo == 1) {

				mListView.setAdapter(mAdapter);
			}
			pageNo++;
			mAdapter.notifyDataSetChanged();
			super.onPostExecute(result);

		}
	}

		}

